<?php
class ControllerModuleTDgiftonorder extends Controller {
	public function index() {
		//$this->language->load('module/td_giftonorder');
		$this->load->model('module/td_giftonorder');
		//Check Gifts by user
		$availableGifts = array();
		$i=0;
		$giftsbyuser = $this->model_module_td_giftonorder->checkGiftsByUser();
		if($giftsbyuser){
			foreach($giftsbyuser as $gift){
				$options = unserialize(base64_decode($gift['options']));
				$gift_cat = $options['gift_catgory'];
				$data = array(
					'filter_category_id' => $gift_cat
				);
				$results = $this->model_module_td_giftonorder->getProducts($gift_cat);
				
				if($results){
					$selected_product = array_values($results)[0];
					if(!isset($this->session->data['gift-'.$gift['id']]))
					{
						//$this->cart->add($selected_product['product_id'], 1, '');
						$key = (int)$product_id . '::';
						$this->session->data['cart'][$key] = 1;
						$this->session->data['gift-'.$gift['id']] = $selected_product['product_id'];
					}
					$availableGifts[$i]['id'] = $gift['id'];
					$availableGifts[$i]['products'] = $results;
					$availableGifts[$i]['name'] = $gift['name'];
					$availableGifts[$i]['message'] = $options['message'];
					$i++;
				}
			}
		}
		//Check Gifts by orders
		$giftsbyorder = $this->model_module_td_giftonorder->checkGiftsByOrder($this->cart->getSubTotal());
		if($giftsbyorder){
			foreach($giftsbyorder as $gift){
				$options = unserialize(base64_decode($gift['options']));
				$gift_cat = $options['gift_catgory'];
				$data = array(
					'filter_category_id' => $gift_cat
				);
				$results = $this->model_module_td_giftonorder->getProducts($gift_cat);
				if($results){
					$selected_product = array_values($results)[0];
					var_dump($selected_product);
					if(!isset($this->session->data['gift-'.$gift['id']]))
					{
						//$this->cart->add($selected_product['product_id'], 1, '');
						$key = (int)$selected_product['product_id'] . '::';
						$this->session->data['cart'][$key] = 1;
						$this->session->data['gift-'.$gift['id']] = $selected_product['product_id'];
					}
					$availableGifts[$i]['id'] = $gift['id'];
					$availableGifts[$i]['products'] = $results;
					$availableGifts[$i]['name'] = $gift['name'];
					$availableGifts[$i]['message'] = $options['message'];
					$i++;
				}
			}
		}
		
		//Check Gifts by products
		$giftsbyproducts = $this->model_module_td_giftonorder->checkGiftsByProducts($this->cart->getProducts());
		if($giftsbyproducts){
			foreach($giftsbyproducts as $gift){
				$options = unserialize(base64_decode($gift['options']));
				$gift_cat = $options['gift_catgory'];
				$data = array(
					'filter_category_id' => $gift_cat
				);
				$results = $this->model_module_td_giftonorder->getProducts($gift_cat);
				if($results){
					$selected_product = array_values($results)[0];
					if(!isset($this->session->data['gift-'.$gift['id']]))
					{
						//$this->cart->add($selected_product['product_id'], 1, '');
						$key = (int)$selected_product['product_id'] . '::';
						$this->session->data['cart'][$key] = 1;
						$this->session->data['gift-'.$gift['id']] = $selected_product['product_id'];
					}
					$availableGifts[$i]['id'] = $gift['id'];
					$availableGifts[$i]['products'] = $results;
					$availableGifts[$i]['name'] = $gift['name'];
					$availableGifts[$i]['message'] = $options['message'];
					$i++;
				}
			}
		}
		$this->data['availableGifts'] = $availableGifts;
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/giftonorder/td_giftonorder.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/giftonorder/td_giftonorder.tpl';
		} else {
			$this->template = 'default/template/module/giftonorder/td_giftonorder.tpl';
		}
		$this->response->setOutput($this->render());
	}
	public function add(){
		
	}
	public function updateGift(){
		$this->cart->add((int)$this->request->post['add']);
		unset($this->session->data['cart'][(int)$this->request->post['remove'].'::']);
		$this->session->data[$this->request->post['gift']] = $this->request->post['add'];
		$this->response->setOutput("Updated");
	}
}
?>